package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * 菜单项管理
 */
@RestController
@RequestMapping("/menu")
@Slf4j
public class MenuController {


    @Reference
    MenuService menuService;

    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu, Integer[] roles){
        log.debug("add : 添加了 {" + menu + "}");
        log.debug("add : 添加了 {" + roles.toString() + "}");
        menuService.add(menu,roles);
        log.debug("新增菜单项成功");
        return new Result(true,"新增菜单项成功");
    }

    //查询所有菜单项
    @RequestMapping("/findAll")
    public Result findAll(){
        log.debug("menu:findAll:查询所有菜单项" );
        List<Menu> menuList = menuService.findAll();
        log.debug("查询菜单项成功");
        return new Result(true,"查询菜单项成功",menuList);
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean){
        log.debug("MenuController-findPage: QueryPageBean{ " + pageBean + " }");
        PageResult pageResult = menuService.findPage(pageBean);
        log.debug("MenuController-findPage: PageResult {" + pageResult + " }");
        return pageResult;
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu,Integer[] roles){
        log.debug("MenuController-edit: menu{ " + menu + " }");
        log.debug("MenuController-edit: roles{ " + Arrays.toString(roles) + " }");
        menuService.edit(menu,roles);
        log.debug("修改菜单项成功");
        return new Result(true,"修改菜单项成功");
    }

    //删除
    @RequestMapping("/delById")
    public Result delById(Integer id){
        log.debug("delById : { " + id +"}" );
        menuService.delById(id);
        log.debug("菜单删除成功");
        return new Result(true,"菜单删除成功");
    }

    //通过id查询一个菜单对象
    @RequestMapping("/findById")
    public Result findById(Integer id){
        log.debug("MenuController-findById: id{ " + id + " }");
        Menu menu = menuService.findById(id);
        log.debug("MenuController-findById: menu: { " + menu + " }");
        return new Result(true, "通过id查询菜单项成功",menu);
    }

}
